package com.techfansy.bottomDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.alltoollib.R;
import com.techfansy.provinceCitySelect.utils.Field;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/26/026.
 */

public class BottomDialog {
    private Context context;
    private boolean isCheck = false;
    /**
     * Display
     * <p>
     * 获取宽高
     */
    private Display display;


    private SheetListener sheetListener;
    /**
     * 取消弹窗
     *
     * @param context
     */
    private TextView tv_cancel;

    /**
     * ScrollView滑动
     *
     * @param context
     */
    private ScrollView scrollView;

    /**
     * dialog弹窗
     *
     * @paramcontext
     */
    public BottomDialog setCancel(SheetListener sheetListener) {
        this.sheetListener = sheetListener;
        return this;
    }

    public BottomDialog setCancel(SheetListener sheetListener, int color) {
        this.sheetListener = sheetListener;
        tv_cancel.setTextColor(color);
        return this;
    }

    private Dialog dialog;
    private TextView tv_title;
    private LinearLayout lLayout_content;
    private List<SheetItem> list;
    private Field field;
    private int height;

    public BottomDialog(Context context) {
        this.context = context;
        float scale = context.getResources().getDisplayMetrics().density;
        height = (int) (scale * 45 + 0.5f);
        WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = window.getDefaultDisplay();
        Builder();
    }

    public BottomDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public BottomDialog setCancelableColor(int color) {
        tv_cancel.setTextColor(color);
        return this;
    }

    public BottomDialog setItemHeight(int height) {
        this.height = height;
        return this;
    }

    public BottomDialog setCanceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    private BottomDialog Builder() {
        field = new Field();
        View view = LayoutInflater.from(context).inflate(field.getLayoutId(context, "dialog_bottom_select"), null);
        view.setMinimumWidth(display.getWidth());
        tv_cancel = (TextView) view.findViewById(field.getViewId(context, "tv_cancel"));

        tv_title = (TextView) view.findViewById(field.getViewId(context, "tv_title"));

        lLayout_content = (LinearLayout) view.findViewById(field.getViewId(context, "lLayout_content"));
        scrollView = (ScrollView) view.findViewById(field.getViewId(context, "sLayout_content"));
        dialog = new Dialog(context, field.getStyleId(context, "ActionSheetDialogStyle"));
        dialog.setContentView(view);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (sheetListener != null)
                    sheetListener.setOnSheet(-1);
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing())
                    dialog.dismiss();
                if (sheetListener != null)
                    sheetListener.setOnSheet(-1);
            }
        });

        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
        return this;
    }

    public BottomDialog setTitle(String name) {
        isCheck = true;
        tv_title.setText(name);
        tv_title.setVisibility(View.VISIBLE);
        return this;
    }

    public BottomDialog setTitleColor(int color) {
        tv_title.setTextColor(color);
        return this;
    }

    public BottomDialog setTitleSize(int size) {
        tv_title.setTextSize(size);
        return this;
    }

    public BottomDialog addItemSheet(int name, SheetListener sheetListener) {
        if (list == null) list = new ArrayList<>();
        list.add(new SheetItem(name, sheetListener, null));
        return this;
    }

    public BottomDialog addCancel(SheetListener sheetListener) {
        this.sheetListener = sheetListener;

        return this;
    }

    public BottomDialog addCancel(SheetListener sheetListener, int color) {
        this.sheetListener = sheetListener;
        tv_cancel.setTextColor(color);
        return this;
    }

    public BottomDialog addItemSheet(int name, SheetListener sheetListener, SheetColor sheetColor) {
        if (list == null) list = new ArrayList<>();
        list.add(new SheetItem(name, sheetListener, sheetColor));
        return this;
    }

    public void show() {
        setSheetView();
        dialog.show();
    }

    private void setSheetView() {
        if (list == null || list.size() <= 0) return;
        int size = list.size();
        if (size >= 7) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) scrollView.getLayoutParams();
            layoutParams.height = display.getHeight() / 2;
            scrollView.setLayoutParams(layoutParams);
        }
        for (int i = 0; i < size; i++) {
            final int index = i;
            final SheetItem sheetItem = list.get(i);
            final SheetListener sheetlistener = sheetItem.sheetListener;
            TextView textView = new TextView(context);
            textView.setText(sheetItem.name);
            textView.setTextSize(18);
            textView.setGravity(Gravity.CENTER);
            if (size == 1) {
                if (isCheck) {//是否存有标题  如有true说明标题存在否就不存在
                    textView.setBackgroundResource(context.getResources().getIdentifier("dialog_bottom_select", "drawable", context.getPackageName()));
                } else
                    textView.setBackgroundResource(getId("dialog_sinle_select", context));
            } else {
                if (isCheck) {
                    if (i < size - 1)
                        textView.setBackgroundResource(getId("dialog_middle_select", context));
                    else textView.setBackgroundResource(getId("dialog_bottom_select", context));
                } else {
                    if (i == 0) textView.setBackgroundResource(getId("dialog_top_select", context));
                    else if (i < size - 1)
                        textView.setBackgroundResource(getId("dialog_middle_select", context));
                    else if (i == size - 1)
                        textView.setBackgroundResource(getId("dialog_bottom_select", context));
                }
                if (i != 0) {
                    TextView textView1 = new TextView(context);
                    textView1.setBackgroundColor(context.getResources().getColor(R.color.actionsheet_gray));
                    textView1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
                    lLayout_content.addView(textView1);
                }
            }
            /**
             * 文字赋予颜色值
             */
            if (sheetItem.sheetColor == null)
                textView.setTextColor(Color.parseColor(SheetColor.Blue.getName()));
            else textView.setTextColor(Color.parseColor(sheetItem.sheetColor.getName()));


            textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    sheetlistener.setOnSheet(index);
                }
            });
            lLayout_content.addView(textView);
        }

    }

    public interface SheetListener {
        void setOnSheet(int index);
    }

    class SheetItem {
        int name;
        SheetListener sheetListener;
        SheetColor sheetColor;

        public SheetItem(int name, SheetListener sheetListener, SheetColor sheetColor) {
            this.sheetColor = sheetColor;
            this.name = name;
            this.sheetListener = sheetListener;
        }
    }

    public enum SheetColor {
        Blue("#2796f0"), Red("#FD4A2E");
        private String name;

        SheetColor(String s) {
            this.name = s;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public int setLayCls(String packageName, String className, String idName) {
        Class<?> cls = null;
        int id = 0;
        try {
            cls = Class.forName(packageName + ".R$layout" + className);
            id = setCls(cls, packageName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return id;
    }

    public int setIdCls(String packageName, String className, String idName) {
        Class<?> cls = null;
        int id = 0;
        try {
            cls = Class.forName(packageName + ".R$id" + className);
            id = setCls(cls, packageName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return id;
    }

    private int getId(String name, Context context) {
        return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
    }

    public int setCls(Class cls, String idName) {
        int id = 0;
        try {
            id = cls.getField(idName).getInt(cls);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return id;
    }


}
